package org.ytor.core.sysapi.depart.api;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;
import org.ytor.common.anno.AutoLog;
import org.ytor.common.anno.Dict;
import org.ytor.common.exception.BaseException;
import org.ytor.common.model.R;
import org.ytor.common.util.Strs;
import org.ytor.common.util.bean.Beans;
import org.ytor.core.BaseController;
import org.ytor.core.querygen.WhereGenerator;
import org.ytor.core.sqlflow.Page;
import org.ytor.core.sqlflow.builder.support.WhereBuilder;
import org.ytor.core.sysapi.depart.logic.SysDepartLogic;
import org.ytor.core.sysapi.depart.model.SysDepart;
import org.ytor.core.sysapi.depart.repository.SysDepartRepository;
import org.ytor.core.sysapi.depart.req.SysDepartReq;
import org.ytor.core.sysapi.depart.resp.SysDepartResp;
import org.ytor.core.util.Pages;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * created by yangtong on 2025年6月2日 21:35:23
 * <br/>
 * 部门表 API
 */
@RestController
@Tag(name = "部门表")
@RequestMapping("/rbac/sysDepart")
@RequiredArgsConstructor
public class SysDepartController extends BaseController<SysDepart, SysDepartLogic, SysDepartRepository> {

    /**
     * 分页查询
     */
    @Dict
    @GetMapping("/page")
    @Operation(summary = "分页查询", description = "分页查询")
    public R<Page<SysDepartResp>> page(@ParameterObject SysDepartReq sysDepartData,
                                       @RequestParam(defaultValue = "1") Long pageNo,
                                       @RequestParam(defaultValue = "10") Long pageSize) {
        WhereBuilder where = WhereGenerator.where();
        Page<SysDepart> page = repository.queryPage(where);
        return R.success(Pages.transPage(page, SysDepart::toResp));
    }

    @Dict
    @GetMapping("/tree")
    @Operation(summary = "返回部门层级数据", description = "返回部门层级数据")
    public R<List<Map<String, Object>>> tree(String departName) {
        return R.success(logic.tree(departName));
    }

    /**
     * 根据id查询数据
     */
    @GetMapping("/queryById")
    @Operation(summary = "根据id查询", description = "根据id查询数据")
    public R<SysDepartResp> queryById(String id) {
        SysDepart sysDepart = repository.queryById(id);
        SysDepartResp resp = null;
        if (sysDepart != null) {
            resp = sysDepart.toResp();
        }
        return R.success(resp);
    }

    /**
     * 新增或编辑数据
     */
    @AutoLog("新增或编辑数据")
    @PostMapping("/addOrUpdate")
    @Operation(summary = "新增或编辑", description = "根据有无主键id,新增或编辑数据")
    public R<String> addOrUpdate(@RequestBody SysDepartReq sysDepartData) {
        SysDepart sysDepart = Beans.copyProperties(sysDepartData, SysDepart.class);
        repository.insertOrUpdate(sysDepart);
        return R.success(sysDepartData.getId() == null ? "新增成功" : "编辑成功");
    }

    /**
     * 删除
     */
    @AutoLog("根据id删除数据")
    @DeleteMapping("/delete/{ids}")
    @Operation(summary = "删除", description = "根据id删除数据")
    public R<String> delete(@PathVariable("ids") String ids) {
        if (Strs.isEmpty(ids)) {
            throw new BaseException("ids不能为空");
        }
        List<String> idList = Arrays.asList(ids.split(","));
        repository.deleteById(idList);
        return R.success("删除成功");
    }

}